#ifndef PCCOREMODEL_H
#define PCCOREMODEL_H

#include <QAbstractListModel>
#include <QVector>
#include <QVariant>
#include <opencv2/opencv.hpp>
#include <vector>
#include "quicktool.h"

// 数据结构
struct Item {
    cv::Mat mat;              // 图像数据
    bool isTrain;             // 是否用于训练
    std::vector<int8_t> data; // 额外数据
    int id;
};

class PCcoreModel : public QAbstractListModel {
    Q_OBJECT

public:
    enum Roles {
        IsTrainRole = Qt::UserRole + 1,
        IdRole
    };

    static PCcoreModel& instance() {
        static PCcoreModel instance;
        return instance;
    }

    GLOBAL_QML_STATE_PROPERTY(int, needTrainDate , 10);

    Q_INVOKABLE void displayImage(int index);

    // 清空函数
    Q_INVOKABLE void clearItems();

    Q_INVOKABLE void removeItem(int index);

    Q_INVOKABLE void confirmItem(bool isConfirmed);
    // 禁止拷贝和赋值
    PCcoreModel(const PCcoreModel&) = delete;
    PCcoreModel& operator=(const PCcoreModel&) = delete;

    // 必须实现的行数接口
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;

    // 必须实现的数据接口
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    // 必须实现的角色接口
    QHash<int, QByteArray> roleNames() const override;

    // 添加数据
    void addItem(const Item &item);
    void addItem(void *result, const cv::Mat &mat);


    int exportEmbedding(QVector<int8_t> &embedding);


    void finishexportEmbedding();

    int countUntrainedItems();
    bool isEmit = false;
    void emitDataCollectionComplete();
    void resetModel();

signals:
    void dataCollectionComplete(int cout);

    void confirmationComplete(); // 发出确认完成信号

private:
    explicit PCcoreModel(QObject *parent = nullptr);
    QVector<Item> m_items;
    int m_currentIndex = -1; // 当前正在处理的项的索引
};

#endif // DATAMODEL_H

